import { Vec2 } from "cc";

export default class MathUtils {

    /**
     * 2个点之前的直线距离
     * @param p1 
     * @param p2 
     */
    public static distance(x1: number, y1: number, x2: number, y2: number) {
        // 设两点A（X1,Y1）,B（X2,Y2）
        // 距离D=（X2-X1）的平方+（Y2-Y1）平方的和开平方
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    /**
     * 弧度转角度 
     * @param radians 
     */
    public static radiansToDegrees(radians: number) {
        return 180 / Math.PI * radians;
    }

    /**
     * 角度转弧度 
     * @param degrees 
     */
    public static degreesToRadians(degrees: number) {
        return Math.PI * degrees / 180;
    }

    /**
     * 返回2点间的弧度
     * @param startP 
     * @param endP 
     */
    public static p2pRad(startP: Vec2, endP:Vec2) {
        let rad: number = Math.atan2(endP.y - startP.y, endP.x - startP.x);
        return rad;
    }

    public static cloneMap(nowMap: Map<any, any>) {
        let result: Map<any, any> = new Map<any, any>();
        nowMap.forEach((value: any, key: any) => {
            result.set(key, value);
        });
        return result;

    }

    public static p2pRotation(startP: Vec2, endP: Vec2) {
        let rad: number = this.p2pRad(startP, endP);
        let rot: number = this.rad2Rot(rad);
        return rot;
    }

    public static p2pRotationCreator(startP: Vec2, endP: Vec2) {
        let rot: number = this.p2pRotation(startP, endP);
        return this.rotation2Creator(rot)
    }

    public static rot2rad(rot: number) {
        let rad: number = Math.PI / 180 * rot;
        return rad;
    }

    public static rad2Rot(rad: number) {
        let rot: number = 180 / Math.PI * rad;
        return rot;
    }

    public static rad2Creator(rad: number) {
        let rot: number = this.rad2Rot(rad);
        return this.rotation2Creator(rot);
    }

    public static rotation2Creator(rot: number) {
        if (rot >= 90) {
            rot = 90 - rot;
        } else if (rot >= -90 && rot < 0) {
            rot = 90 - rot;
        } else if (rot < -90) {
            rot = -270 - rot
        }
        return rot;
    }

    public static rad2Cannon(rad: number) {
        let rot: number = this.rad2Rot(rad);
        return this.rotation2Cannon(rot)
    }

    public static rotation2Cannon(rot: number) {
        rot = 90 - rot;
        return rot;
    }

    public static rad2Fish(rad: number) {
        let rot: number = this.rad2Rot(rad);
        return this.rotation2Fish(rot);
    }

    public static rotation2Fish(rot: number) {
        if (rot >= 0 && rot <= 180) {
            rot = 180 - rot;
        } else {
            rot = -180 - rot;
        }
        return rot;
    }



}
